var pageCount = 50;
var currentPage;

var favsLoadedIds = [];
var favsSelectedIds = [];

$(document).ready(function() {
	currentPage = 1;

    $('#favsLoadingDiv').modal({
    	backdrop : false,
    	keyboard : false,
    	show : false
    });

    $('#favsDeletingDiv').modal({
    	backdrop : false,
    	keyboard : false,
    	show : false
    });
	
	getFavs();

	$(window).scrollTop(0);
});

function getFavs() {
	$.ajax({
		async: true, 
		type: 'GET', 
		dataType: 'json', 
		url: 'action.php', 
		data: {'action' : 'queryFavs', 'page' : currentPage, 'count' : pageCount}, 
		beforeSend: function() {
			$('#favsLoadingDiv').modal('show');
		},
		success: function(data, textFav) {
			$('#favsLoadingDiv').modal('hide');
			
			var totalNumber = data['totalNumber'];
			var favs = data['favs'];

			var totalPage = Math.ceil(totalNumber / pageCount);
			
			if (currentPage > totalPage) {
				if (totalPage > 0) {
					currentPage = totalPage;

					getFavs();
				}
			} else {
				$('#favsCheckbox').prop('checked', false);

				$('#favsDeleteButton').attr('class', 'btn disabled').attr('disabled', 'disabled');
				
				showFavs(favs);

				showPaginations(totalPage);
			}
		}
	});
}

function getFavCheckboxId(favId) {
	return favId + '_checkbox';
}

function getFavDivId(favId) {
	return favId + '_div';
}

function addFavIdToArray(favsIds, favId) {
	favsIds.push(favId);
}

function removeFavIdFromArray(favsIds, favId) {
	var index = -1;
	
	for (var i = 0; i < favsIds.length; i++) {
		if (favId == favsIds[i]) {
			index = i;
			break;
		}
	}

	if (index != -1) {
		favsIds.splice(index, 1);
	}
}

function getArrayFromStr(data) {
	var array = [];
	
	var strArray = data.split(',');

	for (var i = 0; i < strArray.length; i++) {
		var str = strArray[i];
		
		if (str != '') {
			array.push(str);
		}
	}

	return array;
}

function showFavs(favs) {
	$('#favsDiv').html('');
	
	favsLoadedIds = [];
	favsSelectedIds = [];
	
	for (var i = 0; i < favs.length; i++) {
		var fav = favs[i];

		var id = fav['id'];
		
		var statusText = fav['statusText'];
		var statusThumbnailPic = fav['statusThumbnailPic'];
		
		var statusUserId = fav['statusUserId'];
		var statusUserName = fav['statusUserName'];
		var statusUserProfileImageUrl = fav['statusUserProfileImageUrl'];

		var statusThumbnailPicHtml = '';

		if (statusThumbnailPic != undefined && statusThumbnailPic != '') {
			statusThumbnailPicHtml = '<p>' + '<img src=\'' + statusThumbnailPic + '\' />' + '</p>';
		}
		
		var favCheckboxId = getFavCheckboxId(id);
		var favDivId = getFavDivId(id);

		var favHtml = 
			'<div id=\'' + favDivId + '\' class=\'row\' style=\'padding-top: 10px; border-bottom: 1px solid #DDDDDD;\'>'  + 
				'<div class=\'span1\'>' + 
					'<p style=\'text-align: center;\'>' + 
						'<input id=\'' + favCheckboxId + '\' type=\'checkbox\' onclick=\'clickFavCheckbox(' + id + ')\'/>' + 
					'</p>' + 
				'</div>' + 
				'<div class=\'span1\'>' + 
					'<p style=\'text-align: center;\'>' + 
						'<a href=\'http://weibo.com/u/' + statusUserId + '\' title=\'' + statusUserName + '\' target=\'_blank\' >' + 
							'<img src=\'' + statusUserProfileImageUrl + '\' />' + 
						'</a>' + 
					'</p>' + 
				'</div>' + 
				'<div class=\'span8\'>' + 
					'<p>' + 
						'<a href=\'http://weibo.com/u/' + statusUserId + '\' target=\'_blank\' >' + 
							statusUserName + 
						'</a>' + 
					'</p>' + 
					'<p>' + 
						statusText + 
					'</p>' + 
					statusThumbnailPicHtml + 
				'</div>' + 
				'<div class=\'span2\'>' + 
					'<p style=\'text-align: center; \'>' + 
						'<button class=\'btn btn-primary\' onclick=\'clickFavDeleteButton(' + id + ')\'>删除</button>' + 
					'</p>' + 
				'</div>' + 
			'</div>'

		$('#favsDiv').append(favHtml);

		addFavIdToArray(favsLoadedIds, id);
	}
}

function checkFav(favId) {
	var favDivId = getFavDivId(favId);
	
	$('#' + favDivId).css('background', '#D9EDF7');

	addFavIdToArray(favsSelectedIds, favId);

	$('#favsDeleteButton').attr('class', 'btn btn-primary').removeAttr('disabled');

	if (favsSelectedIds.length == favsLoadedIds.length) {
		$('#favsCheckbox').prop('checked', true);
	}
}

function uncheckFav(favId) {
	var favDivId = getFavDivId(favId);
	
	$('#' + favDivId).css('background', '#FFFFFF');

	removeFavIdFromArray(favsSelectedIds, favId);

	if (favsSelectedIds.length == 0) {
		$('#favsDeleteButton').attr('class', 'btn disabled').attr('disabled', 'disabled');
	}

	if (favsSelectedIds.length != favsLoadedIds.length) {
		$('#favsCheckbox').prop('checked', false);
	}
}

function clickFavCheckbox(favId) {
	var favCheckboxId = getFavCheckboxId(favId);

	var favCheckboxChecked = $('#' + favCheckboxId).prop('checked');
	
	if (favCheckboxChecked) {
		checkFav(favId);
	} else {
		uncheckFav(favId);
	}
}

function clickFavsCheckbox(favsCheckboxChecked) {
	if (favsCheckboxChecked) {
		for (var i = 0; i < favsLoadedIds.length; i++) {
			var favId = favsLoadedIds[i];

			var favCheckboxId = getFavCheckboxId(favId);

			$('#' + favCheckboxId).prop('checked', true);
			
			checkFav(favId);
		}
	} else {
		for (var i = 0; i < favsLoadedIds.length; i++) {
			var favId = favsLoadedIds[i];

			var favCheckboxId = getFavCheckboxId(favId);

			$('#' + favCheckboxId).prop('checked', false);
			
			uncheckFav(favId);
		}
	}
}

function deleteFav(favsIds) {
	$.ajax({
		async: true, 
		type: 'POST', 
		dataType: 'text', 
		url: 'action.php', 
		data: {'action' : 'deleteFavs', 'favsIds' : favsIds}, 
		beforeSend: function() {
			$('#favsDeletingDiv').modal('show');
		},
		success: function(data, textFav) {
			$('#favsDeletingDiv').modal('hide');
			
			var deletedFavsIds = getArrayFromStr(data);

			for (var i = 0; i < deletedFavsIds.length; i++) {
				var favId = deletedFavsIds[i];
				
				uncheckFav(favId);
				
				var favDivId = getFavDivId(favId);

				$('#' + favDivId).remove();

				removeFavIdFromArray(favsLoadedIds, favId);

				if (favsLoadedIds.length == 0) {
					$('#paginationDiv ul').html('');
					
					getFavs();
				}
			}
		}
	});
}

function clickFavDeleteButton(favId) {
	deleteFav(favId);
}

function clickFavsDeleteButton() {
	deleteFav(favsSelectedIds.join(','));
}

function clickPagination(page) {
	currentPage = page;

	getFavs();
}

function showPaginations(totalPage) {
	$('#paginationDiv ul').html('');

	if (currentPage == 1) {
		$('#paginationDiv ul').append('<li href=\'#\' class=\'disabled\'><a>&lt;&lt;</a></li>');
	} else {
		$('#paginationDiv ul').append('<li><a href=\'#\' onclick=\'clickPagination(' + (currentPage - 1) + ')\'>&lt;&lt;</a></li>');
	}

	for (var i = 1; i <= totalPage; i++) {
		if (currentPage == i) {
			$('#paginationDiv ul').append('<li href=\'#\' class=\'active\'><a>' + i + '</a></li>');
		} else {
			$('#paginationDiv ul').append('<li><a href=\'#\' onclick=\'clickPagination(' + i + ')\'>' + i + '</a></li>');
		}
	}

	if (currentPage == totalPage) {
		$('#paginationDiv ul').append('<li href=\'#\' class=\'disabled\'><a>&gt;&gt;</a></li>');
	} else {
		$('#paginationDiv ul').append('<li><a href=\'#\' onclick=\'clickPagination(' + (currentPage + 1) + ')\'>&gt;&gt;</a></li>');
	}
}